package com.hlf.modules.merchant.store.controller;

import com.hlf.common.annotation.SysLog;
import com.hlf.common.utils.PageUtils;
import com.hlf.common.utils.ReturnResult;
import com.hlf.common.validator.Assert;
import com.hlf.modules.merchant.bank.entity.BankCard;
import com.hlf.modules.merchant.store.entity.Store;
import com.hlf.modules.merchant.store.service.StoreService;
import com.hlf.modules.sys.controller.AbstractController;
import com.hlf.modules.sys.entity.File;
import com.hlf.modules.sys.entity.SysUserEntity;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * 门店列表
 */
@RestController
@RequestMapping("store/")
public class StoreController extends AbstractController {
    @Autowired
    private StoreService storeService;

    /**
     * 列表
     */
    @RequestMapping("/list")
//	@RequiresPermissions("store:view")
    public ReturnResult list(@RequestParam Map<String, Object> params) {
        PageUtils pageUtil = storeService.queryListForPage(params);
        return ReturnResult.ok().put("page", pageUtil);
    }

    /**
     * 通过id查找门店信息
     */
    @RequestMapping("/info/{storeId}")
    public ReturnResult config(@PathVariable("storeId") String storeId) {
        Store store = storeService.queryObject(Long.parseLong(storeId));
        return ReturnResult.ok().put("store", store);
    }


    /**
     * 新增门店
     */
    @SysLog("新增门店")
    @RequestMapping("/saveStore")
    public ReturnResult save(@RequestBody Store store) {
        Assert.isBlank(store.getName(), "店铺名称不能为空");
        Assert.isNull(store.getBizTypeId(), "经营范围编码不能为空");
        Assert.isBlank(store.getAddress(), "店铺地址不能为空");
        Assert.isBlank(store.getMobilePhone(), "联系电话不能为空");

        List<File> logoImg = store.getLogoImg();
        SysUserEntity user = getUser();
        return storeService.createStore(store,logoImg, user);

      /*  List<File> idCardImg = (List<File>) map.get("idCardImg");
        List<File> businessImg = (List<File>) map.get("businessImg");
        List<File> openImg = (List<File>) map.get("openImg");
        List<File> logoImg = (List<File>) map.get("logoImg");
        map.remove("idCardImg");
        map.remove("businessImg");
        map.remove("openImg");
        map.remove("logoImg");
        String s = JSON.toJSONString(map);
        Store store = null;
        try {
            store = JSON.parseObject(s, Store.class);
        } catch (JSONException e) {
            e.printStackTrace();
            throw new BusinessException("传入数据有误!");
        }
        Assert.isBlank(store.getName(), "店铺名称不能为空");
        Assert.isNull(store.getBizTypeId(), "经营范围编码不能为空");
        Assert.isBlank(store.getAddress(), "店铺地址不能为空");
        Assert.isBlank(store.getMobilePhone(), "联系电话不能为空");
        SysUserEntity user = getUser();
        return storeService.createStore(store, idCardImg, businessImg, openImg, logoImg, user);*/


    }

    /**
     * 修改门店
     */
    @RequestMapping("/updateStore")
    public ReturnResult update(@RequestBody Store store) {
        SysUserEntity user = getUser();
        storeService.updateInfo(user,store);
        return ReturnResult.ok();
    }

    /**
     * 删除门店
     */
    @SysLog("删除门店")
    @RequestMapping("/delete")
    public ReturnResult delete(@RequestBody Long[] storeIds) {

        storeService.deleteBatch(storeIds);
        return ReturnResult.ok();
    }

    /**
     * 修改门店
     */
    @RequestMapping("/saveAuthStore")
    public ReturnResult saveAuthStore(@RequestBody Store store) {
        SysUserEntity user = getUser();
        storeService.updateAuthStore(user,store);
        return ReturnResult.ok();
    }
    /**
     * 修改门店
     */
    @RequestMapping("/getBankCard/{storeId}")
    public ReturnResult getBankCard(@PathVariable("storeId") String storeId) {
        Long id = null;
        if (StringUtils.isNotBlank(storeId)){
            id = Long.valueOf(storeId);
        }
        BankCard bankCard = storeService.getBankCard(id);
        return ReturnResult.ok().put("bankCard",bankCard);
    }

}
